<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Classes;
use App\Admin\Repositories\Student;
use App\Models\Classes as Model;
use App\Models\TeachingMaterial;
use DateTime;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Models\Administrator;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ClassController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        Admin::style(
            <<<CSS
        table {
            text-align: center;
        }
CSS
        );
        $user = Admin::user();
        if ($user->is_admin == -1) {
            $class = $user->classes()->getQuery();
            $classes = new Classes($class);
        } else {
            $classes = new Classes();
        }
        return Grid::make($classes, function (Grid $grid) use ($user) {
            if ($user->is_admin == 1) {
                $grid->model()->with(['mechanism']);
            }
            $grid->column('id')->sortable();
            if ($user->is_admin == 1) {
                $grid->column('mechanism.name', '所属机构');
            }
            $grid->column('name')->display(function ($value) {
                $url = 'classd/'.$this->getKey().'/student';
                return "<a href={$url}>".$value."</a>";
            });
            $grid->column('number');
            $grid->column('teaching_material')->display(function ($value) {
                if (empty($value)) {
                    $result = '无';
                } else {
                    $result = TeachingMaterial::whereIn('id', json_decode($value, true))->select('name')->get();
                    $result = implode(",", $result->pluck('name')->toArray());
                }
                return $result;
            })->label();
            $grid->column('begin_at');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        $user = Admin::user();
        if ($user->is_admin == -1) {
            $classes = new Classes();
        } else {
            $classes = new Classes('mechanism');
        }
        return Show::make($id, $classes, function (Show $show) use ($user) {
            $show->field('id');
            if ($user->is_admin == 1) {
                $show->field('mechanism.name', '所属机构');
            }
            if ($user->is_admin == -1) {
                $show->getKey();
                $book = \App\Models\Classes::find($show->getKey());
                if ($user->is_admin == -1 && $book->mechanism_id != $user->id) {
                    abort(404);
                }
            }
            $show->field('name');
            $show->field('number');
            $show->field('teaching_material');
            $show->field('begin_at');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $user = Admin::user();
        return Form::make(new Classes(), function (Form $form) use ($user) {
            $form->display('id');
            $id = $form->getKey();
            if ($id) {
                $book = \App\Models\Classes::find($id);
                if ($user->is_admin == -1 && $book->mechanism_id != $user->id) {
                    abort(404);
                }
            }
            if ($user->is_admin == 1) {
                $form->select('mechanism_id', '所属机构')
                    ->options(Administrator::where('is_admin', -1)
                        ->pluck('name', 'id')->toArray());
                $teachingMaterial = TeachingMaterial::all()->pluck('name', 'id')->toArray();
            } else {
                $teachingMaterial = TeachingMaterial::where('mechanism_id', $user->id)
                    ->pluck('name', 'id')->toArray();
            }
            $form->text('name');
            $form->multipleSelect('teaching_material')
                ->options($teachingMaterial)
                ->saving(function ($v) {
                    return json_encode($v);
                });
            if ($user->is_admin == -1) {
                $form->hidden('mechanism_id')->default($user->id);
            }
            $form->date('begin_at');
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    public function select(Request $request)
    {
        $provinceId = $request->get('q');

        $class = Model::find($provinceId);
        $ids = json_decode($class->teaching_material, true);
        return TeachingMaterial::whereIn('id', $ids)->get(['id', DB::raw('name as text')]);
    }

    public function studentList(Content $content, Model $book)
    {
        if (Admin::user()->is_admin == -1 && $book->mechanism_id != Admin::user()->id) {
            abort(404);
        }
        $books = $book->students()->getQuery();
        $date = $book->begin_at;
        $date = date("Y-m-d", strtotime($date));
        $contents = Grid::make(new Student($books), function (Grid $grid) use ($date) {
            $datetime_start = new DateTime($date);
            $datetime_end = new DateTime(date('Y-m-d'));
            $cha = ($datetime_start->diff($datetime_end)->days)+1;
            $grid->column('name', '学生名字');
            for ($i=1; $i<=$cha; $i++) {
                $grid->column('data'.$i, $date);
                $date = date("Y-m-d", strtotime("+1 day", strtotime($date)));
            }
            $grid->fixColumns(2);
            $grid->disableActions();
            $grid->disableBatchDelete();
            $grid->disableCreateButton();
            $grid->disableCreateButton();
        });
        return $content
            ->title('教材单元列表')
            ->description('教材单元列表')
            ->body($contents);
    }
}
